import React from 'react';
import {Spin, Card, Table, Row, Col, Button} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import {orderTypeHis, partSaleOrderHisStatus, partOrderPriceTermsHis, paymentHisType, shipmentTypeHis, currencyHis} from '../Enum';
import routes from './routes';
import {TABLE, DATETIME_FORMAT} from '../constants';
import {conventEnumValueToString, formatDateTime} from '../utils';
import {getDetailHis} from './api';
import {injectIntl} from './intl';
import styles from './style.css';
const {Description} = DescriptionList;
class DetailHisPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: {},
        details: [],
        processDetails: []
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getDetailHis(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: res.data,
                        details: res.data.details,
                        processDetails: res.data.processDetails,
                    });
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }
    render() {
        const {data, details, processDetails} = this.state;
        const {formatMessage} = this.props.intl;
        const colums = [{
            title: formatMessage({
                id: 'detailHisPanel.column.partCode',
                defaultMessage: '备件编号'
            }),
            dataIndex: 'partCode',
        }, {
            title: formatMessage({
                id: 'detailHisPanel.column.partName',
                defaultMessage: '备件名称'
            }),
            dataIndex: 'partName',
        }, {
            title: formatMessage({
                id: 'detailHisPanel.column.partEngName',
                defaultMessage: '备件英文名称'
            }),
            dataIndex: 'partEngName',
        }, {
            title: formatMessage({
                id: 'detailHisPanel.column.productCategoryCode',
                defaultMessage: '适用车型'
            }),
            dataIndex: 'productCategoryCode',
        }, {
            title: formatMessage({
                id: 'detailHisPanel.column.minPackSize',
                defaultMessage: '最小包装数'
            }),
            dataIndex: 'minPackSize',
        }, {
            title: formatMessage({
                id: 'detailHisPanel.column.planQuantity',
                defaultMessage: '订购数量'
            }),
            dataIndex: 'planQuantity'
        }, {
            title: formatMessage({
                id: 'detailHisPanel.column.remark',
                defaultMessage: '备注'
            }),
            dataIndex: 'remark'
        }];
        const processColums = [{
            title: formatMessage({
                id: 'detailHisPanel.column.processer',
                defaultMessage: '操作人'
            }),
            dataIndex: 'processer',
        }, {
            title: formatMessage({
                id: 'detailHisPanel.column.processStatus',
                defaultMessage: '操作状态'
            }),
            dataIndex: 'processStatus',
            render: text => conventEnumValueToString(partSaleOrderHisStatus, text)
        }, {
            title: formatMessage({
                id: 'detailHisPanel.column.processComment',
                defaultMessage: '操作意见'
            }),
            dataIndex: 'processComment',
        }, {
            title: formatMessage({
                id: 'detailHisPanel.column.processDate',
                defaultMessage: '操作时间'
            }),
            dataIndex: 'processDate',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }];
        return (
            <div>
                <Spin spinning={this.state.isFetching}>
                    <Card>
                        <DescriptionList title={<Row><Col span={6}>{
                            formatMessage({
                                id: 'detailHisPanel.card.partOtherOrder',
                                defaultMessage: '订单信息'
                            })
                        }</Col></Row>}>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.code',
                                defaultMessage: '订单编号'
                            })}>{data.code}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.dealer',
                                defaultMessage: '总代号'
                            })}>({data.dealerCode}){data.dealerName}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.submitDate',
                                defaultMessage: '提报日期'
                            })}>{data.submitDate}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.country',
                                defaultMessage: '国家/地区'
                            })}>{data.country}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.currency',
                                defaultMessage: '币种'
                            })}>{conventEnumValueToString(currencyHis, data.currency)}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.customerOrderCode',
                                defaultMessage: '客户订单号'
                            })}>{data.customerOrderCode}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.orderTypeHis',
                                defaultMessage: '订单类型'
                            })}>{conventEnumValueToString(orderTypeHis, data.orderTypeHis)}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.paymentHisType',
                                defaultMessage: '付款方式'
                            })}>{conventEnumValueToString(paymentHisType, data.paymentHisType)}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.priceTerms',
                                defaultMessage: '价格条款'
                            })}>{conventEnumValueToString(partOrderPriceTermsHis, data.priceTerms)}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.contactPerson',
                                defaultMessage: '收货联系人'
                            })}>{data.contactPerson}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.contactPhone',
                                defaultMessage: '联系电话'
                            })}>{data.contactPhone}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.account',
                                defaultMessage: '到付账号'
                            })}>{data.account}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.designatedForwarder',
                                defaultMessage: '指定货代'
                            })}>{data.designatedForwarder}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.shipmentType',
                                defaultMessage: '发运方式'
                            })}>{conventEnumValueToString(shipmentTypeHis, data.shipmentType)}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.status',
                                defaultMessage: '状态'
                            })}>{conventEnumValueToString(partSaleOrderHisStatus, data.status)}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.destinationPort',
                                defaultMessage: '目的港口'
                            })}>{data.destinationPort}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.deliveryPort',
                                defaultMessage: '发运港口'
                            })}>{data.deliveryPort}</Description>
                        </DescriptionList>
                        <DescriptionList col="1" className={styles.message}>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.consigneeAddress',
                                defaultMessage: '收货地址'
                            })}>{data.consigneeAddress}</Description>
                            <Description term={formatMessage({
                                id: 'detailHisPanel.operation.remark',
                                defaultMessage: '备注'
                            })}>{data.remark}</Description>
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList title={formatMessage({
                            id: 'detailHisPanel.table.details',
                            defaultMessage: '清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={colums}
                                rowKey="id"
                                pagination={false}
                                dataSource={details}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList title={formatMessage({
                            id: 'detailHisPanel.table.processDetails',
                            defaultMessage: '操作历史'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={processColums}
                                rowKey="id"
                                pagination={false}
                                dataSource={processDetails}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.props.onCancel}>{this.props.intl.formatMessage({
                        id: 'detailHisPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

DetailHisPanel.propTypes = {
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
};

import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = (state, props) => ({
    permissions: getPermissions(state),
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser'])),
});
const mapDispatchToProps = (dispatch, props) => ({
    onCancel: () => {
        props.history.push(routes.hisOrder.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailHisPanel));
